<template>
	<div>
		<van-nav-bar title="让利比例设置" left-arrow @click-left="$utils.back($route.path);" />
		<van-form ref="subForm">
			<van-cell-group class="mt-24 ptb-80 plr-24">
				<!--<van-field v-model="ratio" required label-class="color-gray-light fs-32" type="number" :max="MaxAmount"
					:rules="[{ required: true, pattern: /^(100|([1-9][0-9]?)|(0|[1-9][0-9]?)(\.[\d]{1,2}?))$/, message: '' }]"
					label="让利比例" placeholder="请输入比例" /> <span>%</span>-->
				<van-field v-model="ratio" required label-class="color-gray-light fs-32" :rules="rules" type="number"
					@input="handleInput" label="让利比例" placeholder="请输入比例" /> <span>%</span>
			</van-cell-group>
		</van-form>
		<div class="plr-40 ptb-16 fixed-bottom">
			<van-button size="large" class="bg-physio-primary1 color-white br-44"
				@click="UpdateStoreCashDiscountRatio">保存</van-button>
		</div>
		<van-popup v-model="showTips" :close-on-click-overlay="isclose" class="services">
			<div class="unartificer">
				<div class="fs-28 tac">您没有操作权限</div>
			</div>
		</van-popup>
	</div>
</template>
<script>
import { GetStoreDiscountRatio, UpdateStoreCashDiscountRatio } from 'api/store'
import { GetBusinessFunction } from 'api/businessSetting'
export default {
	components: {

	},
	data() {
		return {
			ratio: '',
			isclose: false,
			showTips: false,
			switch282: 0,
			isRange30: true,
		}
	},
	computed: {
	},
	beforeDestroy() {

	},
	created() {
		this.GetStoreDiscountRatio()
		this.GetBusinessFunction()
	},
	mounted() {

	},
	computed: {
		rules() {
			const max = this.isRange30 ? 30 : 100;
			const pattern = new RegExp(`^(0(\\.\\d{1,2})?|[1-${max - 1}]\\d(\\.\\d{0,2})?|${max})$`);
			return [
				{
					pattern: pattern,
					message: '请输入0-' + max + '之间的数字',
					trigger: 'blur',
				},
			];
		},
	},
	methods: {
		handleInput() {

		},
		GetBusinessFunction() {
			GetBusinessFunction(282).then(res => {
				if (res) {
					this.switch282 = res.Data[0].Value;
					if (this.switch282 === 0) {
						this.isRange30 = false
					} else {
						this.isRange30 = true
					}
				}
			})
		},
		GetStoreDiscountRatio() {
			GetStoreDiscountRatio().then(res => {
				if (res.Data) {
					this.ratio = res.Data.CashDiscountRatio
					if (res.Data.IsStoreSetDiscount === 0) {
						this.showTips = true
					}
				} else {
					this.$router.go(-1)
				}
			})
		},

		UpdateStoreCashDiscountRatio() {
			//this.ratio = parseFloat(this.ratio).toFixed(2);
			this.ratio = Math.round(this.ratio * 100) / 100;
			this.$refs.subForm.validate().then(() => {
				const obj = {}
				obj.cashDiscountRatio = this.ratio
				UpdateStoreCashDiscountRatio(obj).then(res => {
					if (res.Data) {
						this.GetStoreDiscountRatio()
						this.GetBusinessFunction()
						//this.$router.go(-1)
						setTimeout(() => {
							this.$toast('修改成功')
						}, 500);
					}
				})
			})
		}

	}
}
</script>
<style lang="scss" scoped>
/deep/.van-cell-group {
	display: flex;
	align-items: center;
}

/deep/.van-cell {
	align-items: center;
	width: auto;
}

/deep/.van-field__body {
	border: 1px solid;
	width: 150px;
	border-radius: 10px;
	padding: 3px 10px;
}

/deep/.van-field__error-message {
	position: absolute;
}

.van-cell::after {
	border-bottom: none
}

.unartificer {
	background: #fff;
	padding: 24px;
}

.services {
	width: 60%;
	border-radius: 12px;
}
</style>
